<?php

namespace app\sales\controller;

use app\admin\common\Base;
use app\admin\common\Excel;
use think\Db;
//销售收款一览表
class Salereceiptdetail extends Base
{
   public function index()
   {
      $data = $this->request->param();

      if (isset($data['ajax']) && $data['ajax'] == 100) {
         $map = '';
         $page = isset($data['page']) ? $data['page'] : 1;
         $page = intval($page);
         $limit = isset($data['rows']) ? $data['rows'] : 10;
         $limit = intval($limit);
         $start = $limit * ($page - 1);


         if ($map == '') {
            $list = Db::name('order')->where('is_del', 0)->where('enterprise_id', $this->EnterpriseId)->order('id asc')->limit($start, $limit)->select();
            $count = Db::name('order')->where('enterprise_id', $this->EnterpriseId)->where('is_del', 0)->count();
         } else {
            $list = Db::name('order')->where('is_del', 0)->order('id asc')->where('enterprise_id', $this->EnterpriseId)->limit($start, $limit)->select();
            $count = Db::name('order')->where('enterprise_id', $this->EnterpriseId)->where('is_del', 0)->count();
         }

         foreach ($list as $key => $value) {
            $customer = Db::name('customer')->where('id', $value['user_id'])->find();
            $list[$key]['customer_category_name'] = Db::name('customer_category')->where('id', $customer['category_id'])->value('name');
            $list[$key]['customer'] = $customer['name'];
            $list[$key]['saler_name'] = Db::name('auth_user')->where('id', $value['by_admin'])->value('name');
            switch ($value['o_type']) {
               case 0:
                  $list[$key]['o_type'] = '销售订单';
                  break;
               case 1:
                  $list[$key]['o_type'] = '销售单';
                  break;
               case 2:
                  $list[$key]['o_type'] = '销售退货单';
                  break;
               default:
                  # code...
                  break;
            }
            $list[$key]['addtime'] = date('Y-m-d', $value['addtime']);
            $list[$key]['yingshou'] = $value['yh_price'] - $value['payment'];
            $list[$key]['hkl'] = round($value['payment'] / $value['yh_price'] * 100, 2) . '%';
         }
         // halt($list);
         return $this->grid_data($list, $count, $page, $limit);
      }

      return $this->fetch();
   }


   public function add()
   {
      $data = $this->request->param();
      return $this->fetch();
   }

   public function export()
   {
      $data = $this->request->param();
      $rowid = explode(" ", $data['rowid']);
      $title = [
         'customer_category_name' => '客户类别',
         'customer' => '客户',
         'addr' => '客户地址',
         'saler_name' => '销售员',
         'o_type' => '业务类别',
         'addtime' => '单据日期',
         'sn' => '单据编号',
         'saler_user_price' => '销售金额',
         'yh_price' => '优惠后金额',
         'user_cd_price' => '客户承担费用',
         'payment' => '本次收款',
         'yingshou' => '应收款余额',
         'hkl' => '回款率',
         'mark' => '备注',
      ];

      $arr_list = [];

      $list = Db::name('order')->where('is_del', 0)->where('enterprise_id', $this->EnterpriseId)->order('id asc')->select();
      $count = Db::name('order')->where('enterprise_id', $this->EnterpriseId)->where('is_del', 0)->count();

      foreach ($list as $key => $value) {
         $customer = Db::name('customer')->where('id', $value['user_id'])->find();
         $list[$key]['customer_category_name'] = Db::name('customer_category')->where('id', $customer['category_id'])->value('name');
         $list[$key]['customer'] = $customer['name'];
         $list[$key]['saler_name'] = Db::name('auth_user')->where('id', $value['by_admin'])->value('name');
         switch ($value['o_type']) {
            case 0:
               $list[$key]['o_type'] = '销售订单';
               break;
            case 1:
               $list[$key]['o_type'] = '销售单';
               break;
            case 2:
               $list[$key]['o_type'] = '销售退货单';
               break;
            default:
               # code...
               break;
         }
         $list[$key]['addtime'] = date('Y-m-d', $value['addtime']);
         $list[$key]['yingshou'] = $value['yh_price'] - $value['payment'];
         $list[$key]['hkl'] = round($value['payment'] / $value['yh_price'] * 100, 2) . '%';
         array_push($arr_list, $list[$key]);
      }

      // halt($arr_list);
      // array_push($arr_list, $zh);
      // halt($arr_list);
      // array_multisort($distance, SORT_DESC, $arr_list);//按照ID大小排序
      //添加标题到数组头部
      array_unshift($arr_list, $title);
      $kong_arr = $title;
      foreach ($kong_arr as $ke1 => $va1) {
         $kong_arr[$ke1] = '';
      }
      for ($i = 0; $i < 3; $i++) {
         array_unshift($arr_list, $kong_arr);
      }
      //根据此参数获取数据中的值，要与表格标题键名对应
      $keys = [];
      foreach ($title as $k2 => $v2) {
         array_push($keys, $k2);
      }
      $merge = [];
      $options = [];
      $o_type = -1;
      $merge = [ //合并
         'start' => 5,
         'arr_zimu' => ['A', 'B', 'C', 'D', 'E', 'F', 'L', 'M', 'N', 'O', 'P'],
         'keyword' => 'sn',
      ];
      $options = [
         'alignCenter' => ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P'], //居中
         'bold' => ['A1:P3', 'A4:P4'], //加粗
         'fontSize' => ['zimu' => ['A1'], 'size' => 20], //字体大小
      ];
      $title = [ //标题
         'title' => "销售收款一览表",
         'max_zimu' => "P",
         'number' => '3',
      ];
      $excel_export_title = "销售收款一览表_" . date('Y-m-d_His') . '.xlsx'; //文件名称
      $excel = new Excel();
      $excel->exportExcel($arr_list, $keys, $excel_export_title, $options, $o_type, $merge, $title);

      exit();
   }
}
